// pages/me/me.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    show:false,
    token:0,
    color: "rgba(119, 119, 119,0.8)",
    viewId: 1,
    id: '',
    x: '',
    addipt: '',
    num: 0,
    username: "",
    headerurl: "",
    masklayerbool: false,
    masklayerbools: false,
    masklayerboolss: false,
    masklayerboolsss: false,
    // 顶部
    topbool: false,
    animation: '',
    animations: '',
    animationss: '',
    animationsss: '',
    idx: '',
    arr5: [],
    songcode: '',
    shu:'',
    likeimage:'',
    several:0,
    // 功能数组
    arr: [{
      img: "icon-download01",
      text: "本地存储"
    }, {
      img: "icon-yunpan",
      text: "云盘"
    }, {
      img: "icon-yigoushuliang",
      text: "已购"
    }, {
      img: "icon-bofang1",
      text: "最近播放"
    }],

    arr1: [{
      img: "icon-haoyou",
      text: "我的好友"
    }, {
      img: "icon-shoucang1",
      text: "收藏和赞"
    }, {
      img: "icon-bokeyuan",
      text: "我的博客"
    }, {
      img: "icon-icon--",
      text: "音乐应用"
    }],
    arr3: [],
    arr4: [],
    nums: 0
  },
  // 前往个人歌单
  tomyself(e){
    wx.navigateTo({
      url: `../personlist/personlist?id=${e.currentTarget.dataset.id}`
    })
  },
  // 前往喜欢
  like(){
    if(wx.getStorageSync('token')==1){
        wx.navigateTo({
      url: `../like/like`
    })
    }else{
      wx.showModal({
        title:"用户未登入",
        cancelColor: 'cancelColor',
      })
    }
  
  },
  // 前往收藏歌单
  tosonglist(e){
    wx.navigateTo({
      url: `../details/details?id=${e.currentTarget.dataset.id}&songsheetCode=${e.currentTarget.dataset.songsheetcode}`})
  },
  // 如果未登入跳转到登入页面
  login(){
    wx.navigateTo({
      url: '../login/login',
    })
  },
  // 点击创建歌单或收藏时的特效
  navclick: function (e) {
    if (e.currentTarget.dataset.idex == 1) {
      wx.pageScrollTo({
        scrollTop: 345,
        duration: 300
      })
    }
    if (e.currentTarget.dataset.idex == 2) {
      wx.pageScrollTo({
        scrollTop: 549,
        duration: 300
      })
    }
    this.setData({
      viewId: e.currentTarget.dataset.idex
    })
  },
  // 获取滚动条当前位置
  onPageScroll: function (e) {
    if (e.scrollTop >= 70) {
      this.setData({
        topbool: true
      })
    } else {
      this.setData({
        topbool: false
      })
    }
    if (e.scrollTop >= 341) {
      this.setData({
        x: 'songlistnav1'
      })
    } else {
      this.setData({
        x: ""
      })
    }
  },
  // 我的详情页
  userinfo() {
    wx.navigateTo({
      url: '../userinfo/userinfo',
    })
  },
  // 添加歌单输入框
  shuru(e) {
    if (e.detail.value != "") {
      this.setData({
        color: "#333"
      })
    } else {
      this.setData({
        color: "rgba(119, 119, 119,0.8)"
      })
    }
    this.setData({
      num: e.detail.cursor,
      addipt: e.detail.value
    })
  },

  // 添加歌单
  add() {
    let x=wx.getStorageSync('token')
    if(x==1){
      this.animation.bottom(0).step()
      this.setData({
        animation: this.animation.export(),
        masklayerbool: true
      })
    }else{
      wx.showModal({
        title:"用户未登入",
        cancelColor: 'cancelColor',
      })
    }
  },
  // 遮罩
  masklayer() {
    this.animation.bottom(-300).step()
    this.setData({
      animation: this.animation.export(),
      masklayerbool: false,
    })
  },
  // 取消
  del() {
    this.animation.bottom(-300).step()
    this.setData({
      animation: this.animation.export(),
      masklayerbool: false
    })
  },
  // 点击按钮添加歌单
  finish() {
    if (this.data.addipt != "") {
      this.animation.bottom(-300).step()
      this.setData({
        animation: this.animation.export(),
        masklayerbool: false
      })
      wx.request({
        url: 'http://192.168.0.26:8081/songSheet/addSongSheet',
        method: 'POST',
        data: {
          songsheetName: this.data.addipt,
          userId: wx.getStorageSync('userid')
        },
        success: (res) => {
          this.onLoad()
        }
      })
    }
  },

  // 动画
  menushow(e) {
    this.animation.left(0).step();
    this.setData({
      animationData: this.animation.export()
    })
  },
  menuhide(e) {
    this.animation.left("-100%").step();
    this.setData({
      animationData: this.animation.export()
    })
  },


  //创建新歌单 
  new() {
    this.animation.bottom(0).step()
    this.setData({
      animation: this.animation.export(),
      masklayerbool: true
    })
    this.animation.bottom(-500).step()
    this.setData({
      animations: this.animation.export(),
      masklayerbools: false,
    })
  },
  

  // 创建歌单三个点
  dians() {
    let x=wx.getStorageSync('token')
    if(x==1){
      this.animation.bottom(0).step()
      this.setData({
        animations: this.animation.export(),
        masklayerbools: true
      })
    }else{
      wx.showModal({
        title:"用户未登入",
        cancelColor: 'cancelColor',
      })
    }
  },
  masklayers() {
    this.animation.bottom(-500).step()
    this.setData({
      animations: this.animation.export(),
      masklayerbools: false,
    })
  },

  // 单个歌单三个点
  dian(e) {
    this.animation.bottom(0).step()
    this.setData({
      animationss: this.animation.export(),
      masklayerboolss: true,
      idx: e.currentTarget.dataset.idx
    })
    wx.setStorageSync("id", e.currentTarget.dataset.id)
  },
  masklayerss() {
    this.animation.bottom(-500).step()
    this.setData({
      animationss: this.animation.export(),
      masklayerboolss: false,
    })
  },


  // 删除歌单
  delt(e) {
    let id = wx.getStorageSync('id')
    wx.request({
      url: `http://192.168.0.26:8081/songSheet/deleteSongSheetById?id=${id}`,
      method: "POST",
      success: (res) => {
        this.animation.bottom(-500).step()
        this.setData({
          animationss: this.animation.export(),
          masklayerboolss: false,
        })
        this.onLoad();
      }
    })
  },

  // 收藏歌单（取消收藏）
  xiaodian(e) {
    this.animation.bottom(0).step()
    this.setData({
      animationsss: this.animation.export(),
      masklayerboolsss: true,
      songcode: e.currentTarget.dataset.songcode
    })
  },
  masklayersss() {
    this.animation.bottom(-500).step()
    this.setData({
      animationsss: this.animation.export(),
      masklayerboolsss: false,
    })
  },
  deltcollect() {
    wx.request({
      url: 'http://192.168.0.26:8081/userCollect/updateUserCollect',
      method: "POST",
      data: {
        userCode: wx.getStorageSync('userCode'),
        songsheetCode: this.data.songcode
      },
      success: (res) => {
        this.animation.bottom(-500).step()
        this.setData({
          animationsss: this.animation.export(),
          masklayerboolsss: false,
        })
        this.onLoad()
      }
    })
  },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.animation = wx.createAnimation({
      duration: 200,
    })
    var s=wx.getStorageSync('token')
    if(s==1){
      // 用户信息
      wx.request({
        url: 'http://192.168.0.26:8081/user/getUserByCode',
        method: 'post',
        data: {
          userCode: wx.getStorageSync('userCode'),
        },
        success: (res) => {
          wx.setStorageSync('userid',res.data.data.id)
          if(res.data.data.headerurl!=""&&res.data.data.headerurl!=null){
            this.setData({
              headerurl: `http://192.168.0.26:8081${res.data.data.headerurl}`
            })
          }else{
            this.setData({
              headerurl:''
            })
          }
          if (res.data.data.userName == "") {
            this.setData({
              username: res.data.data.phone
            })
          } else {
            this.setData({
              username: res.data.data.userName
            })
          }
          if (res.data.data.headerurl == "") {
            this.setData({
              headerurl: '../image/panda.jpg'
            })
          } else {
            this.setData({
            })
          }
        }
      })


       // 用户喜欢
    wx.request({
      url: 'http://192.168.0.26:8081/userLike/getUserLikeByUserCode',
      method:'post',
      data:{
        userCode:wx.getStorageSync('userCode'),
      },
      success:(res)=>{
        let arr=res.data.data
        if(arr.length!=0){
          let image=`http://192.168.0.26:8081${arr[0].imgUrl}`
          this.setData({
          likeimage:image
          })
        }
        this.setData({
          several:arr.length,
        })
      }
    })

    // 用户创建歌单
    wx.request({
      url: 'http://192.168.0.26:8081/songSheet/getSongSheetByUserId',
      method: "GET",
      data: {
        id: wx.getStorageSync('userid')
      },
      success: (res) => {
        var arr = JSON.parse(JSON.stringify(res.data.data))
        this.setData({
          arr5: arr,
          shu:arr.length
        })
        for (let i = 0; i < res.data.data.length; i++) {
          if (res.data.data[i].imgUrl != null && res.data.data[i].imgUrl != "") {
            res.data.data[i].imgUrl = "http://192.168.0.26:8081" + res.data.data[i].imgUrl
          }
        }
        this.setData({
          arr4: res.data.data,
        })
      }
    })


    // 收藏歌单
    wx.request({
      url: 'http://192.168.0.26:8081/userCollect/getUserCollectByUserCode',
      method: 'POST',
      data: {
        userCode: wx.getStorageSync('userCode'),
      },
      success: (res) => {
   
        this.setData({
          arr3: res.data.data,
          nums: res.data.data.length
        })
      }
    })

      
    // 动画
    this.animation = wx.createAnimation({
      delay: 100,
      duration: 500,
      timingFunction: 'linear',
      transformOrigin: "left top 0"
    })
    }

   

  },
  // 编辑
  compile() {
    let arr = this.data.arr5[this.data.idx]
    let imagesrc = arr.imgUrl;
    let songsheetName = arr.songsheetName
    wx.navigateTo({
      url: `../compile/compile?imagesrc=${imagesrc}&songsheetName=${songsheetName}&id=${this.data.id}`,
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
   
     var s=wx.getStorageSync('token');
     this.setData({
       token:s
     })

    if(s==1){
      this.setData({
        show:true
      })
      // 用户信息
      wx.request({
        url: 'http://192.168.0.26:8081/user/getUserByCode',
        method: 'post',
        data: {
          userCode: wx.getStorageSync('userCode'),
        },
        success: (res) => {
          wx.setStorageSync('userid',res.data.data.id)
          if(res.data.data.headerurl!=""&&res.data.data.headerurl!=null){
            this.setData({
              headerurl: `http://192.168.0.26:8081${res.data.data.headerurl}`
            })
          }else{
            this.setData({
              headerurl:''
            })
          }
          if (res.data.data.userName == "") {
            this.setData({
              username: res.data.data.phone
            })
          } else {
            this.setData({
              username: res.data.data.userName
            })
          }
          if (res.data.data.headerurl == "") {
            this.setData({
              headerurl: '../image/panda.jpg'
            })
          } else {
            this.setData({
            })
          }
        }
      })

       // 用户喜欢
    wx.request({
      url: 'http://192.168.0.26:8081/userLike/getUserLikeByUserCode',
      method:'post',
      data:{
        userCode:wx.getStorageSync('userCode'),
      },
      success:(res)=>{
        let arr=res.data.data
        console.log("1",arr);
        if(arr.length!=0){
          let image=`http://192.168.0.26:8081${arr[0].imgUrl}`
          this.setData({
          likeimage:image
          })
        }
        this.setData({
          several:arr.length,
        })
      }
    })

    // 用户创建
    wx.request({
      url: 'http://192.168.0.26:8081/songSheet/getSongSheetByUserId',
      method: "GET",
      data: {
        id: wx.getStorageSync('userid')
      },
      success: (res) => {
    
        // var arr = JSON.parse(JSON.stringify(res.data.data))
        // this.setData({
        //   arr5: arr,
        //   shu:arr.length
        // })
        
        for (let i = 0; i < res.data.data.length; i++) {
          if (res.data.data[i].imgUrl != null && res.data.data[i].imgUrl != "") {
            res.data.data[i].imgUrl = "http://192.168.0.26:8081" + res.data.data[i].imgUrl
          }
        }
        this.setData({
          arr4: res.data.data,
        })
   
        this.onLoad()
      }
    })
 
    

    // 收藏歌单
    wx.request({
      url: 'http://192.168.0.26:8081/userCollect/getUserCollectByUserCode',
      method: 'POST',
      data: {
        userCode: wx.getStorageSync('userCode'),
      },
      success: (res) => {
        this.setData({
          arr3: res.data.data,
          nums: res.data.data.length
        })
      }
    })

      
    // 动画
    this.animation = wx.createAnimation({
      delay: 100,
      duration: 500,
      timingFunction: 'linear',
      transformOrigin: "left top 0"
    })

    }
    else {
      this.setData({
        show:false
      })
      this.setData({
        arr4:[],
        // arr5: [],
        shu:0,
        arr3:[],
        nums:0,
        likeimage:'',
        several:0
      })
    }

   
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    this.animation.left("-100%").step();
    this.setData({
      animationData: this.animation.export()
    })
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})